package cn.com.request;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "RequestParamsServlet", value = "/RequestParamsServlet")
public class RequestParamsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("utf-8");
        String name = request.getParameter("username");//获取请求中的用户名参数
        String password = request.getParameter("password"); //获取请求中的密码参数
        System.out.println("用户名：" + name);//打印输出用户名
        System.out.println("密码：" + password);//打印输出密码
//获取参数名为"hobby”的值，这是一个数组类型的参数
        String[] hobbys = request.getParameterValues("hobby");//获取请求中的爱好参数数组
        System.out.print("爱好:"); //打印输出爱好标签
        for (int i = 0; i < hobbys.length; i++) {//遍历爱好数组
            System.out.print(hobbys[i] + ",");//打印输出每个爱好，并用逗号分隔
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }


}
